package com.unipay.hosp.api;

import com.alibaba.excel.util.StringUtils;
import com.unipay.common.handler.UnipayException;
import com.unipay.common.helper.HttpRequestHelper;
import com.unipay.common.result.Result;
import com.unipay.common.result.ResultCodeEnum;
import com.unipay.common.utils.MD5;
import com.unipay.hosp.service.DepartmentService;
import com.unipay.hosp.service.HospitalService;
import com.unipay.hosp.service.HospitalSetService;
import com.unipay.hosp.service.ScheduleService;
import com.unipay.model.hosp.Department;
import com.unipay.model.hosp.Hospital;
import com.unipay.model.hosp.Schedule;
import com.unipay.vo.hosp.DepartmentQueryVo;
import com.unipay.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Project: yitong_parent
 * @Author: C.yu
 * @Time: 2023-04-02 21:31:10
 * @Desc: TODO
 */
@Api(tags = "Hosp管理API接口")
@RequestMapping(value = "/api/hosp")
@RestController
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //删除 Schedule 接口
    @ApiOperation(value = "删除 Schedule")
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //校验
        String hoscode = (String) paramObjectMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new UnipayException(ResultCodeEnum.PARAM_ERROR);
        }
        String requestSign = (String) paramObjectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String MD5SignKey = MD5.encrypt(signKey);
        if (!requestSign.equals(MD5SignKey)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        String hosScheduleId = (String) paramObjectMap.get("hosScheduleId");
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    //查询 Schedule 接口
    @ApiOperation(value = "查询 Schedule")
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取：请求参数
        Map<String, Object> paramObjMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //校验
        String hoscode = (String) paramObjMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new UnipayException(ResultCodeEnum.PARAM_ERROR);
        }
        String requestSign = (String) paramObjMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encryptSignKey = MD5.encrypt(signKey);
        if (!requestSign.equals(encryptSignKey)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        //改造查询条件
        //Object page = paramObjMap.get("page");
        int page = StringUtils.isEmpty(paramObjMap.get("page")) ? 1 : Integer.parseInt((String) paramObjMap.get("page"));
        int limit = StringUtils.isEmpty(paramObjMap.get("limit")) ? 10 : Integer.parseInt((String) paramObjMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        String depcode = (String) paramObjMap.get("depcode");
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> schedulePage = scheduleService.selectSchedulePage(page, limit, scheduleQueryVo);
        return Result.ok(schedulePage);
    }

    //添加 Schedule 接口
    @ApiOperation(value = "添加 Schedule")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取：请求参数
        Map<String, Object> paramObjMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //校验
        String hoscode = (String) paramObjMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new UnipayException(ResultCodeEnum.PARAM_ERROR);
        }
        String requestSign = (String) paramObjMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encryptSignKey = MD5.encrypt(signKey);
        if (!requestSign.equals(encryptSignKey)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramObjMap);
        return Result.ok();
    }

    //删除 Dep 接口
    @ApiOperation(value = "删除Dep")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //校验
        String hoscode = (String) paramObjectMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new UnipayException(ResultCodeEnum.PARAM_ERROR);
        }
        String requestSign = (String) paramObjectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String MD5SignKey = MD5.encrypt(signKey);
        if (!requestSign.equals(MD5SignKey)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        String depcode = (String) paramObjectMap.get("depcode");
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //查询 Dep 接口
    @ApiOperation(value = "查询Dep")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, Object> paramObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //校验
        String hoscode = (String) paramObjectMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new UnipayException(ResultCodeEnum.PARAM_ERROR);
        }
        String requestSign = (String) paramObjectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String MD5SignKey = MD5.encrypt(signKey);
        if (!requestSign.equals(MD5SignKey)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        //获取：当前页、每页数
        int page = StringUtils.isEmpty(paramObjectMap.get("page")) ? 1 : Integer.parseInt((String) paramObjectMap.get("page"));
        int limit = StringUtils.isEmpty(paramObjectMap.get("limit")) ? 10 : Integer.parseInt((String) paramObjectMap.get("limit"));
        //条件参数
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        String depcode = (String) paramObjectMap.get("depcode");
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageDepartment = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageDepartment);
    }

    //上传 Dep 接口
    @ApiOperation(value = "上传Dep")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //校验
        String hoscode = (String) objectMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new UnipayException(ResultCodeEnum.PARAM_ERROR);
        }
        String requestSign = (String) objectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String MD5SignKey = MD5.encrypt(signKey);
        if (!requestSign.equals(MD5SignKey)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(objectMap);
        return Result.ok(objectMap);
    }

    //查询Hosp接口
    @ApiOperation(value = "查询Hosp")
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request) {
        //获取：请求参数信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //sign、hoscode校验
        String hoscode = (String) objectMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new UnipayException(ResultCodeEnum.PARAM_ERROR);
        }
        String requestSign = (String) objectMap.get("sign");
        String sign = hospitalSetService.getSignKey(hoscode);
        String md5Sign = MD5.encrypt(sign);
        if (!requestSign.equals(md5Sign)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospitalByHoscode = hospitalSetService.getHospitalByHoscode(hoscode);
        return Result.ok(hospitalByHoscode);
    }

    //上传Hosp接口
    @ApiOperation(value = "上传Hosp")
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取：manage系统上传过来请求参数的hosp对象。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);     //why？？？方便后面JSON转实体对象

        //签名校验---相应的Hosp，才能调用对应的接口
        //（1）获取参数里面上传对象的sign（已MD5加密）
        String requestSign = (String) paramMap.get("sign");
        //（2）根据参数里面上传对象的hoscode，去查询service_hosp系统的Hosp设置hospital_set表，的签名
        String hoscode = (String) paramMap.get("hoscode");
        String sign = hospitalSetService.getSignKey(hoscode);
        //（3）将库里查出的sign加密MD5后，和上传的sign进行比较
        String md5Sign = MD5.encrypt(sign);

        //不同，发送异常
        if (!requestSign.equals(md5Sign)) {
            throw new UnipayException(ResultCodeEnum.SIGN_ERROR);
        }
        //TODO 图片base64编码，说明：Hosplogo图片小，因此上传Hosplogo是可以使用base64格式保存
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataStr = (String) paramMap.get("logoData");
        logoDataStr = logoDataStr.replaceAll(" ", "+");
        paramMap.put("logoData", logoDataStr);
        //相同，则调用service的方法，进行 “保存或更改” 操作
        hospitalService.save(paramMap);
        return Result.ok();
    }

}
